package v1

import (
	"github.com/gin-gonic/gin"
	"go-admin/global"
	mr "go-admin/internal/model/api/model_request"
	ms "go-admin/internal/model/api/model_response"
	"go-admin/internal/service"
	"go-admin/pkg/app"
	"go-admin/pkg/errcode"
)

type Role struct {
}

func NewRole() Role {
	return Role{}
}

// 创建
func (r *Role) Create(c *gin.Context) {
	param := mr.CreateRoleRequest{}
	response := app.NewResponse(c)
	valid, errors := app.BindAndValid(c, &param)
	if !valid {
		// 打印日志
		global.Logger.Errorf("app.BindAndValid errs: %v", errors)
		response.Fail(app.WithMsg(errors.Errors()...))
		return
	}
	svc := service.New(c.Request.Context())
	insertId, err := svc.CreateRole(&param)
	if err != nil {
		global.Logger.WithCaller(2).Errorf("svc.CreateRole err: %v", err)
		response.Fail(app.WithMsg(errcode.ErrCreate))
		return
	}
	response.Ok(app.WithData(app.ToId(insertId)))
	return
}

// 获取列表
func (r *Role) List(c *gin.Context) {
	param := mr.ListRoleRequest{}
	response := app.NewResponse(c)
	valid, errors := app.BindAndValid(c, &param)
	if !valid {
		// 打印日志
		global.Logger.WithCaller(2).Errorf("app.BindAndValid errs: %v", errors)
		response.Fail(app.WithMsg(errors.Errors()...))
		return
	}
	svc := service.New(c.Request.Context())

	// 获取列表总数
	totalRows, err := svc.CountRole(&param)
	if err != nil {
		// 打印日志
		global.Logger.WithCaller(2).Errorf("svc.CountRole errs: %v", errors)
		response.Fail(app.WithMsg(errcode.ErrGet))
		return
	}
	// 获取分页信息
	pager := app.Pager{PageIndex: app.GetPageIndex(c), PageSize: app.GetPageSize(c), TotalRows: totalRows}
	list, err := svc.GetRoleList(&param, &pager)
	if err != nil {
		// 打印日志
		global.Logger.WithCaller(2).Errorf("svc.GetRoleList errs: %v", errors)
		response.Fail(app.WithMsg(errcode.ErrGet))
		return
	}
	response.Ok(app.WithData(app.ToPageList(pager, ms.ModelToListRoleRes(list))))
	return
}
func (r *Role) Update(c *gin.Context) {
	param := mr.UpdateRoleRequest{}
	response := app.NewResponse(c)
	valid, errors := app.BindAndValid(c, &param)
	if !valid {
		// 打印日志
		global.Logger.Errorf("app.BindAndValid errs: %v", errors)
		response.Fail(app.WithMsg(errors.Errors()...))
		return
	}
	svc := service.New(c.Request.Context())
	if err := svc.UpdateRole(&param); err != nil {
		global.Logger.WithCaller(2).Errorf("svc.UpdateRole err: %v", err)
		response.Fail(app.WithMsg(errcode.ErrEdit))
		return
	}
	response.Ok()
	return
}

func (r *Role) RoleDetailById(c *gin.Context) {
	param := mr.CommonId{}
	response := app.NewResponse(c)
	valid, errors := app.BindAndValid(c, &param)
	if !valid {
		// 打印日志
		global.Logger.WithCaller(2).Errorf("app.BindAndValid errs: %v", errors)
		response.Fail(app.WithMsg(errors.Errors()...))
		return
	}
	svc := service.New(c.Request.Context())
	role, err := svc.GetRoleById(param.Id)
	if err != nil {
		// 打印日志
		global.Logger.WithCaller(2).Errorf("app.GetJobById errs: %v", err)
		response.Fail(app.WithMsg(errcode.ErrGet))
		return
	}
	response.Ok(app.WithData(app.ToDetail(role)))
}

// 批量删除
func (r *Role) Deletes(c *gin.Context) {
	param := mr.CommonIds{}
	response := app.NewResponse(c)
	valid, errors := app.BindAndValid(c, &param)
	if !valid {
		// 打印日志
		global.Logger.Errorf("app.BindAndValid errs: %v", errors)
		response.Fail(app.WithMsg(errors.Errors()...))
		return
	}
	svc := service.New(c.Request.Context())
	if err := svc.DeleteRoleS(param.Ids); err != nil {
		// 打印日志
		global.Logger.WithCaller(2).Errorf("svc.DeleteRoleS errs: %v", errors)
		response.Fail(app.WithMsg(errcode.ErrDelete))
		return
	}
	response.Ok()
	return
}

func (r *Role) ListAll(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	list, err := svc.RoleListAll()
	if err != nil {
		// 打印日志
		global.Logger.Errorf("svc.ListAll errs: %v", err)
		response.Fail(app.WithMsg(errcode.ErrGet))
		return
	}
	response.Ok(app.WithData(app.ToList(ms.ModelToListRoleRes(list))))
	return
}
